<template>
	<div class="addRule">
		<!-- 规则 -->
		<el-form :model="addData" style="margin-left:20px;" :rules="rules" ref="ruleForm">
			<el-form-item label="规则名称:" prop="ruleName">
			  <el-input v-model="addData.ruleName"></el-input>
			</el-form-item>
			<el-form-item label="适用车场:">
			  <el-radio v-model="addData.ifAllParks" :label="1" @change="selectParkShow = false">全部停车场</el-radio>
  			<el-radio v-model="addData.ifAllParks" :label="0" @change="selectParkShow = true">部分停车场</el-radio>
      </el-form-item>
      <el-form-item v-if="selectParkShow" class="parkList">
        <template v-for="item in Letter">
          <span :class="{active: actived == item}" @click="getAllPark(item)">{{item}}</span>
        </template>
        <span class="allSeletc" @click="ifAllParks = true;getAllPark()" :indeterminate="isIndeterminate">A-Z全选</span>
        <span class="allSeletc" @click="empty">清空</span>
        <hr/>
        <template>
          <el-checkbox-group class="parkNames" v-model="addData.parkIds" v-for="item in parkList">
            <el-checkbox :label="item.id" :disabled="ruleParkIds.indexOf(item.id) > -1" >{{ item.parkName }}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-form-item>
			<el-form-item>
			  <template v-for="(item, index) in addData.monthRentTypes">
      		<p>
      			<label>充值规则 {{index + 1}}</label>
				  	<el-input class="rul" v-model="item.amount">
				  		<template slot="append">元</template>
				    </el-input>
				    <el-input class="rul" v-model="item.duration">
				  		<template slot="append">小时</template>
				    </el-input>
				    <span class="adrul" @click="reduceRule(index)">-</span>
			    </p>
    		  </template>
    		  <span class="adrul" v-if="addShow" @click="addRule">+</span>
    		  <span class="ruletips">最多可选4个</span>
			</el-form-item>
      <el-form-item label="备注描述:">
        <el-input type="textarea" :rows="2" v-model="addData.remark" style="width: 400px;">
        </el-input>
      </el-form-item>
			<el-form-item>
				<p style="text-align: center;margin-left: -20px;">
          <el-button>取消</el-button>
					<el-button type="warning" @click="confirmAdd('ruleForm')">确认</el-button>
				</p>
		    </el-form-item>
    </el-form>
	</div>
</template>

<script>
export default{
	name:'addRule',
	data(){
		return{
      checkList: [],
      actived: 'A',
      Letter: ["A","B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z"],
      addData: {
        monthRentTypes: [{
          amount:'',
          duration:'',
        }],
        parkIds: [],
        ifAllParks: 0
      },
      parkList: [],
      count: 1,
      selectParkShow: true,
			addShow:true,
      ifAllParks: false,
      isIndeterminate: true,
      ruleParkIds: [],
      rules: {
        ruleName: { required: true, message: '请输入规则名称', trigger: 'blur' },

      }
		}
	},
	mounted(){
    this.getDurationRuleParkList();
    this.getAllPark('A');

	},
	methods:{
    //根据首字母查询车场名称展示
    getAllPark(letter) {
      this.parkList = [];
      this.actived = letter;
      this.$axios({
        method: 'get',
        url: '/park/park-info/getParkList',
      }).then((res) => {
          res.data.forEach(item => {
            if(this.ifAllParks) {
              this.parkList.push(item);
            } else {
              if(item.parkNameFirstSpell == letter) {
                this.parkList.push(item)
              }
            }
          })
          //全选排除掉已有规则的车场
          if(this.ifAllParks) {
            let ruleIds = this.ruleParkIds
            let arr = [];
            res.data.forEach(item => {
              arr.push(item.id)
            })
            let diff = arr.filter(function (val) { return ruleIds.indexOf(val) === -1 });
            this.addData.parkIds = diff;
          }
          this.ifAllParks = false;
      })
    },

    //获取已有规则的停车场
    getDurationRuleParkList() {
      this.$axios({
        method: 'get',
        url: '/promotion/duration-rule-park/getDurationRuleParkList',
      }).then((res) => {
        res.data.forEach((rulePark) => {
          this.ruleParkIds.push(rulePark.parkId);
        })
      })
    },

    //清空选中的车场
    empty() {
      this.addData.parkIds = [];
    },

		//增加规则
		addRule(){
			this.addData.monthRentTypes.push({
				amount:'',
				duration:''
			})
			this.count ++;
			this.countNum();
		},

		//减去规则
		reduceRule(index){
			this.addData.monthRentTypes.splice(index,1)
			this.count --;
			this.countNum();
		},

		//计数当规则数量等于4时隐藏增加键
		countNum(){
			if( this.count >= 4){
				this.addShow = false;
			}else{
				this.addShow = true;
			}
		},

		//确认新增规则接口
		confirmAdd(formName){
      let flag = this.addData.monthRentTypes.every(item => {
         if(item.amount == null || item.amount == '') {
           this.$message.error('规则金额不能为空')
           return false
         } else if (item.duration == null || item.duration == '') {
           this.$message.error('规则时长不能为空')
           return false
         } else {
           return true
         }
      })
      if(this.addData.ifAllParks == 0) {
        if(this.addData.parkIds.length == 0) {
          this.$message.warning('请选择停车场！')
        } else {
          this.$refs[formName].validate((valid) => {
            if (valid && flag) {
              this.$axios({
                method: 'post',
                url: '/promotion/duration-rule/addDurationRule',
                data: this.addData
              }).then((res) => {
                if(res.code == 200) {
                  this.$message.success('新增规则成功！');
                  this.$emit('closeDialog');
                  this.$emit('refresh')
                } else {
                  this.$message.error('新增规则失败！')
                }
              })
            } else {
              return false;
            }
          });
        }
      }
		},
	}
}
</script>

<style scoped>
.el-input{
	width: 300px;
}
.rul{
	width: 130px;
	margin:0 6px;
}
.adrul{
	font-size: 24px;
	color: red;
	float: right;
	margin-right: 50px;
}
.adrul:hover{
	cursor: pointer;
}
.ruletips{
	margin-left: 60px;
	color: #aaa;
}
.parkList {
  height: 200px;
  width: 670px;
  overflow: scroll;
}
.parkList span {
  padding: 6px;
}
.parkList span:hover {
  cursor: pointer;
}
.parkNames {
  display: inline-block;
  width: 150px;
  padding: 0;
  overflow: hidden;
}
.active {
  background-color: #1890FF;
  border-radius: 5px;
  color: #fff;
}
.allSeletc {
  background-color: #09CCDE;
  border-radius: 3px;
}
</style>
